//557. 反转字符串中的单词 III

#include <algorithm>
#include <string>
using namespace std;

class Solution {
public:
    string reverseWords(string s) {
        int len = s.size();
        int i = 0;
        while(i < len)
        {
            int start = i;
            while(i < len && s[i] != ' ')//先判断越界
            {
                ++i;
            }

            int left = start, right = i-1;
            while(left < right)
            {
                swap(s[left], s[right]);//首尾交换

                ++left;
                --right;
            }

            while(i < len && s[i] == ' ')//处理连续的空格
            {
                ++i;  
            }
        }
        return s;
    }
};